package cn.huihe.demoai.controller;

import cn.huihe.demoai.common.Result;
import cn.huihe.demoai.common.SwaggerBearerAuth;
import cn.huihe.demoai.common.WebResponse;
import cn.huihe.demoai.vendor.weka.IrisPredicateRequest;
import cn.huihe.demoai.vendor.weka.RegressionApi;
import cn.huihe.demoai.vendor.weka.SimpleSummary;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SwaggerBearerAuth
@RestController
@RequestMapping("/v1/regression")
@Tag(name = "Regression API")
public class RegressionController {

    private final RegressionApi regressionApi;

    public RegressionController(RegressionApi regressionApi) {
        this.regressionApi = regressionApi;
    }

    @GetMapping("/logistic")
    @Operation(
            summary = "logistic regression example",
            responses = {@ApiResponse(responseCode = "200")},
            parameters = {
                    @Parameter(name = "ratio", description = "Ratio of training datasets", example = "0.7")
            }
    )
    public ResponseEntity<Result<SimpleSummary>> logisticRegression(@RequestParam(name = "ratio", required = false) Double trainingRatio) throws Exception {
        return WebResponse.ok(regressionApi.logisticRegression(trainingRatio));
    }

    @GetMapping("/logistic/predicted")
    @Operation(
            summary = "logistic regression predicted example",
            responses = {@ApiResponse(responseCode = "200")},
            parameters = {
                    @Parameter(name = "sepalLength", description = "length of sepal", example = "5.1"),
                    @Parameter(name = "sepalWidth", description = "width of sepal", example = "3.5"),
                    @Parameter(name = "petalLength", description = "length of petal", example = "1.4"),
                    @Parameter(name = "petalWidth", description = "width of petal", example = "0.2")
            }
    )
    public ResponseEntity<Result<String>> logisticRegressionPredicted(@RequestParam(value = "sepalLength") double sepalLength,
                                                                      @RequestParam(value = "sepalWidth") double sepalWidth,
                                                                      @RequestParam(value = "petalLength") double petalLength,
                                                                      @RequestParam(value = "petalWidth") double petalWidth) throws Exception {
        return WebResponse.ok(regressionApi.logisticPredicted(IrisPredicateRequest.of(sepalLength, sepalWidth, petalLength, petalWidth)));
    }

}
